package com.hongxin.nw.netty.server;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.mq_info.domain.AuthRequest;
import com.hongxin.nw.netty.utils.HttpClient4;
import com.hongxin.nw.netty.utils.MyJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


/**
 * @remark 业务系统的服务
 * @author: GuoXiaolin
 * @date 2019/6/25 12:00
 * @Version 1.0
 * @Descibe
 */
@Slf4j
@Service
public class AuthService {

    //请求方式POST
    private final static String POST = "POST";
    //请求方式GET
    private final static String GET = "GET";

    /**
     * 设备签到接口
     */
    @Value("${nw.sign}")
    private String SIGN_IN;

    /**
     * 卡鉴权
     */
    @Value("${nw.verify}")
    private String VERIFY;

    /**
     * 獲取设备计费策略
     */
    @Value("${nw.findPolicy}")
    private String findPolicy;

    /**
     * 鉴权
     * @return 返回
     */
    public MyJson auth(AuthRequest authRequest) throws Exception {
        String jsonString = JSONObject.toJSONString(authRequest);
        String httpRes;
        try {
            httpRes = HttpClient4.doPost(VERIFY, POST, jsonString);
            log.info("鉴权请求运营系统返回数据:{}", httpRes);
        } catch (Exception e) {
            throw new Exception("鉴权接口故障,请联系李爱兴同志解决");
        }
        return new MyJson(httpRes);
    }

    /**
     * 获取计费策略
     * @return 返回
     */
    public MyJson findPolicy(String code) throws Exception{
//        Map<String, Object> paramMap = new ConcurrentHashMap<>();
//        paramMap.put("machineId", code);
        String httpRes;
        try {
            httpRes = HttpClient4.doPost(findPolicy, POST, code);
            log.info("获取设备默认计费策略返回数据:{}", httpRes);
        } catch (Exception e) {
            throw new Exception("获取设备默认计费策略接口故障,请联系李爱兴同志解决");
        }
        return new MyJson(httpRes);
    }

    /**
     * 设备签到
     * @return 返回 myJson
     */
    public MyJson signIn(String code) throws Exception{
        String httpRes;
        try {
            httpRes = HttpClient4.doPost(SIGN_IN, POST, code);
            log.info("设备签到返回数据:{}", httpRes);
        } catch (Exception e) {
            throw new Exception("设备签到接口故障,请联系李爱兴同志解决");
        }
        return new MyJson(httpRes);
    }


}
